package com.oa.demo.auth.service.impl;

import com.oa.demo.auth.entity.TMenu;
import com.oa.demo.auth.mapper.TMenuMapper;
import com.oa.demo.auth.service.ITMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.oa.demo.common.exception.BaseAuthException;

/**
* <p>
*  服务实现类
* </p>
*
* @author kongbox
* @since 2020-11-27
*/
@Slf4j
@Service
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu> implements ITMenuService {

    @Override
    public Page<TMenu> listTMenusByPage(int page, int pageSize, String factor) {
    log.info("正在执行分页查询tMenu: page = {} pageSize = {} factor = {}",page,pageSize,factor);
    QueryWrapper<TMenu> queryWrapper =  new QueryWrapper<TMenu>().like("", factor);
    //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
    Page<TMenu> result = super.page(new Page<>(page, pageSize));
    log.info("分页查询tMenu完毕: 结果数 = {} ",result.getRecords().size());
    return result;
    }

    @Override
    public TMenu getTMenuById(int id) {
    log.info("正在查询tMenu中id为{}的数据",id);
    TMenu tMenu = super.getById(id);
    log.info("查询id为{}的tMenu{}",id,(null == tMenu?"无结果":"成功"));
    return tMenu;
    }

    @Override
    public int insertTMenu(TMenu tMenu) throws BaseAuthException {
    log.info("正在插入tMenu");
    if (super.save(tMenu)) {
    log.info("插入tMenu成功,id为{}",tMenu.getId());
    return tMenu.getId();
    } else {
    log.error("插入tMenu失败");
    throw new BaseAuthException("添加失败");
    }
    }

    @Override
    public int deleteTMenuById(int id) throws BaseAuthException {
    log.info("正在删除id为{}的tMenu",id);
    if (super.removeById(id)) {
    log.info("删除id为{}的tMenu成功",id);
    return id;
    } else {
    log.error("删除id为{}的tMenu失败",id);
    throw new BaseAuthException("删除失败[id=" + id + "]");
    }
    }

    @Override
    public int updateTMenu(TMenu tMenu) throws BaseAuthException {
    log.info("正在更新id为{}的tMenu",tMenu.getId());
    if (super.updateById(tMenu)) {
    log.info("更新d为{}的tMenu成功",tMenu.getId());
    return tMenu.getId();
    } else {
    log.error("更新id为{}的tMenu失败",tMenu.getId());
    throw new BaseAuthException("更新失败[id=" + tMenu.getId() + "]");
    }
    }

    }
